package com.b1502.store2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.b1502.store2.R;
import com.b1502.store2.activity.ProductDetailActivity;
import com.b1502.store2.adapter.HomeProductAdapter;
import com.b1502.store2.model.AdvertItem;
import com.b1502.store2.model.Product;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.GlideImageLoader;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends BaseFragment implements XRecyclerView.LoadingListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private Gson gson;
    private View view;
    private Banner home_head_banner;
    private XRecyclerView home_frag_recyclerView;
    private List<Product> mList = new ArrayList<>();

    public HomeFragment() {
        // Required empty public constructor
    }


    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_home, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        home_head_banner = (Banner) view.findViewById(R.id.home_head_banner);
        home_frag_recyclerView = (XRecyclerView) view.findViewById(R.id.home_frag_recyclerView);
        home_frag_recyclerView.setPullRefreshEnabled(true);
        home_frag_recyclerView.setLoadingMoreEnabled(true);
        home_frag_recyclerView.setLoadingListener(this);
        gson = new Gson();
        getAdvertItems();
        getHomeProducts(0);
    }

    private void getHomeProducts(int pageIndex) {
        StoreParams params = new StoreParams(UrlUtil.GetHomeProducts, pageIndex);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                List<Product> productList = new ArrayList<>();
                Product[] products = gson.fromJson(result, Product[].class);
                for (int i = 0; i < products.length; i++) {
                    productList.add(products[i]);
                }
                home_frag_recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));
                mList.addAll(productList);
                HomeProductAdapter homeProductAdapter = new HomeProductAdapter(getActivity(), mList);
                home_frag_recyclerView.setAdapter(homeProductAdapter);
                homeProductAdapter.setRecyItemClickListener(new HomeProductAdapter.RecyItemClickListener() {
                    @Override
                    public void addRecyItemClickListener(int pos) {
                        Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                        intent.putExtra("productId",mList.get(pos).getProductId());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    /**
     * 请求轮播图图片
     */
    private void getAdvertItems() {
        StoreParams params = new StoreParams(UrlUtil.GetAdvertItems);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                List<String> imageList = new ArrayList<>();
                AdvertItem[] advertItems = gson.fromJson(result, AdvertItem[].class);
                for (int i = 0; i < advertItems.length; i++) {
                    String imageUrl = UrlUtil.getImageUrl(advertItems[i].imgUrl);
                    imageList.add(imageUrl);
                }
                home_head_banner.setImages(imageList).setImageLoader(new GlideImageLoader()).start();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    } //停止加载更多和刷新
    private void stopRefreshAndLoad(){
        home_frag_recyclerView.refreshComplete();
        home_frag_recyclerView.loadMoreComplete();
    }


    @Override
    public void onRefresh() {
        getHomeProducts(0);
        stopRefreshAndLoad();
    }

    @Override
    public void onLoadMore() {
        stopRefreshAndLoad();
    }
}
